package org.domain.acount.session;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.domain.acount.entity.Conta;
import org.domain.acount.entity.Historico;
import org.domain.acount.entity.Transacao;
import org.jboss.seam.Component;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.contexts.Contexts;
import org.jboss.seam.framework.EntityQuery;
import org.richfaces.component.html.HtmlExtendedDataTable;
import org.richfaces.component.html.HtmlTree;
import org.richfaces.event.NodeSelectedEvent;
import org.richfaces.model.selection.Selection;

@Name("transacaoList")
public class TransacaoList extends EntityQuery<Transacao> {

	@In EntityManager entityManager;

	private static final String EJBQL = "select transacao from Transacao transacao where transacao.contaIdConta = #{transacaoList.contaFilter}";

	private static final String[] RESTRICTIONS = {
		"(transacao.data) >= (#{transacaoList.dataDe})",
		"(transacao.data) <= (#{transacaoList.dataA})",
		"lower(transacao.historico) like lower(concat(#{transacaoList.transacao.historico},'%'))",};

	private List<Transacao> parcelaList = new ArrayList();
	private Transacao transacao = new Transacao();
	private Integer contaFilter = new Integer(0);	
	private Integer filtroConta = new Integer(0);
	private Date dataA;
	private Date dataDe;
	
	@SuppressWarnings("unchecked")
	public List<Historico> historicoAutoComplete(Object o) {
		String sql =  "select historico from Historico historico where historico.idConta = " + getContaFilter() + " and lower(historico.historico) like concat('%',lower('"+o.toString()+"'),'%')";
		List<Historico> resultList = this.getEntityManager().createQuery(sql).getResultList();
   		return resultList;   
	}
	
	@Temporal(TemporalType.DATE)
	public Date getDataDe() {
		if (Contexts.getSessionContext().get("dataDe") != null) {
			return (Date) Contexts.getSessionContext().get("dataDe");
		}		
		return dataDe;
	}

	public void setDataDe(Date dataDe) {
		Contexts.getSessionContext().set("dataDe", dataDe);		
		this.dataDe = dataDe;
	}

	@Temporal(TemporalType.DATE)
	public Date getDataA() {
		if (Contexts.getSessionContext().get("dataA") != null) {
			return (Date) Contexts.getSessionContext().get("dataA");
		}	
		return dataA;
	}

	public void setDataA(Date dataA) {
		Contexts.getSessionContext().set("dataA", dataA);
		this.dataA = dataA;
	}	
	
	private String nomeCompletoConta;

	public String getNomeCompletoConta() {
		if (nomeCompletoConta == null && contaFilter != null) {
			Conta conta = getContaById(getContaFilter());
			setNomeCompletoConta(generateNomeCompleto(conta));
		}
		return nomeCompletoConta;
	}

	private Conta getContaById(int contaFilter2) {
		Query query = entityManager.createNativeQuery("SELECT * FROM conta WHERE id_conta = ? order by nome", Conta.class);
		query.setParameter(1, contaFilter2);
		Conta conta = (Conta) query.getSingleResult();
		return conta;
	}

	public void setNomeCompletoConta(String nomeCompletoConta) {
		this.nomeCompletoConta = nomeCompletoConta;
	}

	public int getContaFilter() {
		if (Contexts.getSessionContext().get("contaFilter") != null) {
			return (Integer) Contexts.getSessionContext().get("contaFilter");
		}	
		else {
			contaFilter = new Integer(0);
			return contaFilter;
		}	
			
	}

	public void setContaFilter(int contaFilter) {
		Contexts.getSessionContext().set("contaFilter", contaFilter);
		this.contaFilter = contaFilter;
	}

	public Integer getFiltroConta() {
		if (Contexts.getSessionContext().get("filtroConta") != null) {
			return (Integer) Contexts.getSessionContext().get("filtroConta");
		}	
		else {
			filtroConta = new Integer(0);
			return filtroConta;
		}	
			
	}

	public void setFiltroConta(Integer filtroConta) {
		Contexts.getSessionContext().set("filtroConta", filtroConta);
		this.filtroConta = filtroConta;
	}

	
	public TransacaoList() {
		setEjbql(EJBQL);
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
	}

	
	public Transacao getTransacao() {
		return transacao;
	}
	

	public String listenerLoadTransactions(NodeSelectedEvent event){
		HtmlTree htmlTree = (HtmlTree) event.getComponent();
		Conta conta = (Conta) htmlTree.getRowData();
		setContaFilter(conta.getIdConta());
		setNomeCompletoConta(generateNomeCompleto(conta));
		TransacaoHome transacaoHome = (TransacaoHome) Component.getInstance("transacaoHome");
		transacaoHome.getInstance().setContaIdConta(conta.getIdConta());
		ContaHome contaHome = (ContaHome) Component.getInstance("contaHome");
		contaHome.setContaIdConta(conta.getIdConta());
		contaHome.load();
		return "SUCCESS";
	}
	
	public String filtraConta(){
		Conta conta = getContaById(getContaFilter());
		Contexts.getSessionContext().set("contaFilter", conta.getIdConta());
		setNomeCompletoConta(generateNomeCompleto(conta));
		TransacaoHome transacaoHome = (TransacaoHome) Component.getInstance("transacaoHome");
		transacaoHome.getInstance().setContaIdConta(conta.getIdConta());
		ContaHome contaHome = (ContaHome) Component.getInstance("contaHome");
		contaHome.setContaIdConta(conta.getIdConta());
		contaHome.load();		
	return "SUCCESS";
	}
	
	public String reset(){
		transacao.setHistorico(null);
		transacao.setContaCredito(null);
		setDataA(null);
		setDataDe(null);
		return "SUCCESS";
	}	
	
	
	

	private String generateNomeCompleto(Conta conta) {
		String nomeConta = "";		
		if (conta != null) {
		nomeConta = conta.getNome();
		// faz loop da conta até chegar na conta pai 0
	   	Query query;
		while (conta.getContaIdConta() != 0) {
			query = entityManager.createNativeQuery("SELECT * FROM conta WHERE id_conta = ? order by nome", Conta.class);
			query.setParameter(1, conta.getContaIdConta());
			conta = (Conta) query.getSingleResult();
			nomeConta = conta.getNome() + " - " + nomeConta;
		}
		}

		return nomeConta;
	}
	




	
}